<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2019/5/9
 * Time: 下午3:48
 */

namespace App\Http\Services;


use App\Http\Model\AnalysisStat;
use App\Http\Model\Hint;

class AnalysisStatService extends CommonServices
{
    protected $modelStat;


    public function __construct(){
        $this->modelStat = new AnalysisStat();
    }

    public function getRoomFlow(){
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);

        do {
            $this->I('room_id', '房间ID', 'int', true);

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化
            $res = $this->modelStat->getRoomFlow($this->requestData['room_id']);
            foreach ($res as $r){
                if(date('Y-m-d' ,strtotime($r['statis_time'])) == date('Y-m-d' ,strtotime('-1 days'))){
                    $data['yesterday'] = $r;
                }else{
                    $data['history'][] = $r;
                }
            }

        } while (false);

        return compact('code', 'msg', 'data');
    }

    public function insertRoomFlow(){
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            $this->I('room_id', '房间ID', 'int', true);
            $this->I('joinNum', '进厅人数', 'int', true);
            $this->I('mengxin', '萌新数量', 'int', false);
            $this->I('consumerNum', '消费人数', 'int', false);
            $this->I('dayofStream', '日流水(元)', 'int', false);
            $this->I('openTime', '开厅时长(h)', 'int', false);
            $this->I('publicChat', '公屏聊天人数', 'int', false);
            $this->I('privateNum', '扣私信人数', 'int', false);
            $this->I('privateMXNum', '给萌新扣私信人数', 'int', false);
            $this->I('statis_time', '统计时间', 'string', false);

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化
            $this->requestData['statis_time'] = $this->requestData['statis_time'] ?? date('Y-m-d H:i:s');
            $this->modelStat->insertRoomFlow($this->requestData);

        } while (false);

        return compact('code', 'msg', 'data');
    }
}
